/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.custom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ProgressBar;

import com.ecarx.systemui.plugin.utils.LogUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class VerticalSeekBar extends androidx.appcompat.widget.AppCompatSeekBar {

    private static final String TAG = "VerticalSeekBarTag";

    private OnSeekBarChangeListener seekBarChangeListener;

    public VerticalSeekBar(Context context) {
        super(context);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @SuppressWarnings("SuspiciousNameCombination")
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);
        super.onDraw(c);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
//                checkSeekBarChangeListener();
                if (seekBarChangeListener != null) {
                    seekBarChangeListener.onStartTrackingTouch(this);
                }
                setProgressByUser((int) (getMax() - (getMax() * event.getY() / getHeight())));
                break;
            case MotionEvent.ACTION_MOVE:
                setProgressByUser((int) (getMax() - (getMax() * event.getY() / getHeight())));
                break;
            case MotionEvent.ACTION_UP:
//                checkSeekBarChangeListener();
                if (seekBarChangeListener != null) {
                    seekBarChangeListener.onStopTrackingTouch(this);
                }
                setProgressByUser((int) (getMax() - (getMax() * event.getY() / getHeight())));
                break;
            default:
                return super.onTouchEvent(event);
        }
        return true;
    }


    public void setProgressByUser(int progress) {
//        callParentProgressBarM(progress, true);
        onSizeChanged(getWidth(), getHeight(), 0, 0);
    }

    public void setProgressNoCallBack(int progress) {
//        callParentProgressBarM(progress, false);
        onSizeChanged(getWidth(), getHeight(), 0, 0);
    }

    private Method setProgressInternal;

//    private void callParentProgressBarM(int progress, boolean isFromUser) {
//        try {
//            if (setProgressInternal == null) {
//                setProgressInternal = getProgressInternalM();
//            }
//            if (setProgressInternal == null) {
//                return;
//            }
//            setProgressInternal.invoke(this,
//                    progress, isFromUser, false);
//        } catch (IllegalAccessException | InvocationTargetException e) {
//            LogUtils.e(TAG, "callParentProgressBarM Error :" + e.getMessage());
//        }
//    }

//    @SuppressWarnings("JavaReflectionMemberAccess")
//    private Method getProgressInternalM() {
//        try {
//            Class<ProgressBar> clazz = ProgressBar.class;
//            @SuppressLint("DiscouragedPrivateApi")
//            Method method = clazz.getDeclaredMethod("setProgressInternal",
//                    int.class, boolean.class, boolean.class);
//            method.setAccessible(true);
//            return method;
//        } catch (NoSuchMethodException e) {
//            LogUtils.e(TAG, "callParentProgressBarM Error :" + e);
//        }
//        return null;
//    }


//    @Suppres-sWarnings("JavaReflectionMemberAccess")
//    private void checkSeekBarChangeListener() {
//        try {
//            Class<?> clazz = Class.forName("android.widget.SeekBar");
//            @SuppressLint("DiscouragedPrivateApi")
//            Field field = clazz.getDeclaredField("mOnSeekBarChangeListener");
//            field.setAccessible(true);
//            seekBarChangeListener = (OnSeekBarChangeListener) field.get(this);
//        } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
//            e.printStackTrace();
//        }
//    }

}
